package com.guyu.app.admin.pojo.converter;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.guyu.app.admin.controller.response.DistrictRsp;
import com.guyu.app.common.enums.DistrictLevelEnum;
import com.guyu.app.common.pojo.domain.ds.DistrictDO;
import com.guyu.app.common.util.TinyPinyinUtil;
import com.guyu.common.core.enums.YesOrNoEnum;
import com.guyu.common.core.util.GyStreamUtils;
import org.dromara.hutool.extra.pinyin.engine.tinypinyin.TinyPinyinEngine;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

public class DistrictConverter {

    public static DistrictDO init(String name, String adcode, DistrictLevelEnum level, String parentCode, String path, String version) {
        if (StrUtil.isBlank(name)) {
            return null;
        }
        name = StrUtil.removeAny(name, "(地区)", "(万柳地区)", "(渔阳地区)", "（省级）", "(省级)", "(特殊乡镇)",
                "直辖地域", "(镇级单位)", "（泗县当涂现代产业园）", "（挪步园）", "（武昌）", "(民主礁)", "(中心团场)", "(平山岛)", "(达念山)");

        TinyPinyinEngine engine = TinyPinyinUtil.getEngine();

        DistrictDO districtDO = new DistrictDO();
        districtDO.setName(name);
        districtDO.setAdcode(adcode);
        districtDO.setCenter("");
        districtDO.setLevel(level.getCode());
        districtDO.setCityCode("");
        districtDO.setParentCode(parentCode);
        districtDO.setPath(path);
        districtDO.setPinyin(engine.getFirstLetter(name, ""));
        districtDO.setHot(YesOrNoEnum.NO.getCode());
        districtDO.setVersion(version);
        districtDO.setCreateTime(LocalDateTime.now());
        districtDO.setUpdateTime(districtDO.getCreateTime());
        districtDO.setDeleted(YesOrNoEnum.NO.getCode());
        return districtDO;
    }

    public static DistrictRsp toRsp(DistrictDO districtDO) {
        if (Objects.isNull(districtDO)) {
            return null;
        }
        DistrictRsp districtRsp = new DistrictRsp();
        districtRsp.setAdcode(districtDO.getAdcode());
        districtRsp.setName(districtDO.getName());
        districtRsp.setCenter(districtDO.getCenter());
        districtRsp.setPinyin(districtDO.getPinyin());
        districtRsp.setLevel(districtDO.getLevel());
        districtRsp.setDistricts(Lists.newArrayList());
        return districtRsp;
    }

    public static List<DistrictRsp> toRspList(List<DistrictDO> list) {
        return GyStreamUtils.toList(list, DistrictConverter::toRsp);
    }
}
